/**
 * 
 */
package org.t2.backstage.module.player;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.backstage.interceptor.RoleContext;
import org.gaming.backstage.module.user.service.UserService;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.player.model.ForbidIp;
import org.t2.backstage.module.player.query.ForbidIpQuery;
import org.t2.backstage.module.player.service.ForbidIpService;

import sh.t2.share.apiutil.GameApi;

/**
 * @author YY
 */
@Controller
public class ForbidIpController {

	@Autowired
	private ForbidIpService forbidIpService;
	@Autowired
	private UserService userService;
	
	@ApiDocument("请求封禁IP数据")
	@RequestMapping(value = "/forbidIp/data.auth")
	@ResponseBody
	public PageData<ForbidIp> data(ForbidIpQuery query) {
		QuerySet querySet = new QuerySet();
		if(query.getIp() != null && !"".equals(query.getIp())) {
			querySet.addCondition("ip like ?", "%" + query.getIp() + "%");
		}
		querySet.orderBy("order by id desc");
		querySet.limit(query.getPage(), query.getLimit());
		querySet.formWhere();
		
		return forbidIpService.query(querySet);
	}
	
	@ApiDocument("添加封禁IP")
	@RequestMapping(value = "/forbidIp/add.authw")
	@ResponseBody
	public ForbidIp save(@RequestParam("ip") String ip, @RequestParam("remark") String remark) {
		RoleContext roleContext = userService.getCurrUser();
		
		ForbidIp forbidIp = new ForbidIp();
		forbidIp.setIp(ip);
		forbidIp.setRemark(remark);
		forbidIp.setUserId(roleContext.getId());
		forbidIp.setUserName(roleContext.getName());
		forbidIpService.insert(forbidIp);
		
		return forbidIp;
	}
	
	@ApiDocument("删除封禁IP")
	@RequestMapping(value = "/forbidIp/delete.authw")
	@ResponseBody
	public void delete(@RequestParam("ip") String ip) {
		ForbidIp forbidIp = forbidIpService.getEntity(0, ForbidIp.ipToLong(ip));
		forbidIpService.delete(forbidIp);
	}
	
	@ApiDocument("是否为封禁IP，给游戏服的接口")
	@RequestMapping(value = GameApi.isForbidIp)
	@ResponseBody
	public String isWhiteList(@RequestParam("ip") String ip) {
		ForbidIp forbidIp = forbidIpService.getEntity(0, ForbidIp.ipToLong(ip));
		if(forbidIp != null) {
			return "true";
		} else {
			return "false";
		}
	}
}
